package com.itlaobing.response.headers;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
/*
理解HTTP响应头的作用并设置content-type字段的值
 */
@WebServlet( "/content" )
public class ContentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String type = request.getParameter("type");
        this.log(type);
        String cont = request.getContentType();
        this.log(cont);

        if ("html".equalsIgnoreCase(type)){
            response.setHeader("content-type","text/html ; charset=utf-8");
        }else if ("text".equalsIgnoreCase(type)){
            response.setHeader("content-type","text/plain ;charset=utf-8");
        }else {
            //空
        }

        PrintWriter out = response.getWriter();
        out.println( "<h3 style=\"text-align : center ;\">" );
        out.println( LocalDateTime.now() );
        out.println( "</h3>" );

    }
}
